opendir(DH,"/Users/yaping/Documents/workspace/result/ASM/") or die;
my $num=$ARGV[0];
my @normal_p;
my @tumor_p;
my @normal_p_adjust;
my @tumor_p_adjust;
my @normal_methy;
my @tumor_methy;
my $p_normal_title="track type=bedGraph name=\"normal P value\" description=\"normal P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $p_tumor_title="track type=bedGraph name=\"tumor P value\" description=\"tumor P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $p_adjust_normal_title="track type=bedGraph name=\"normal adjust P value\" description=\"normal adjust P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $p_adjust_tumor_title="track type=bedGraph name=\"tumor adjust P value\" description=\"tumor adjust P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $methy_normal_title="track type=bedGraph name=\"normal Methylation ratio\" description=\"tumor Methylation ratio\" visibility=full color=255,255,0 altColor=0,255,255 priority=20";
my $methy_tumor_title="track type=bedGraph name=\"tumor Methylation ratio\" description=\"tumor Methylation ratio\" visibility=full color=255,255,0 altColor=0,255,255 priority=20";
push(@normal_p,"$p_normal_title\n");
push(@normal_methy,"$methy_normal_title\n");
push(@tumor_p,"$p_tumor_title\n");
push(@tumor_methy,"$methy_tumor_title\n");
push(@normal_p_adjust,"$p_adjust_normal_title\n");
push(@tumor_p_adjust,"$p_adjust_tumor_title\n");

foreach my $file(readdir(DH)){
	if($file=~/methylCGsRich_ASM_AllSnp_(\w+)Merge_(\w+)_pValue_ASMblock.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</Users/yaping/Documents/workspace/result/ASM/$file");
		my @in=<FH>;
		chomp(@in);
		close(FH);
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			next if $splitin[2] < $num;
			my @range=split "~",$splitin[1];
			if($sample eq "normal"){
				push(@normal_p,"$chr\t$range[0]\t$range[1]\t$splitin[4]\n");
				push(@normal_methy,"$chr\t$range[0]\t$range[1]\t$splitin[3]\n");
				push(@normal_p_adjust,"$chr\t$range[0]\t$range[1]\t$splitin[5]\n");
			}
			else{
				push(@tumor_p,"$chr\t$range[0]\t$range[1]\t$splitin[4]\n");
				push(@tumor_methy,"$chr\t$range[0]\t$range[1]\t$splitin[3]\n");
				push(@tumor_p_adjust,"$chr\t$range[0]\t$range[1]\t$splitin[5]\n");
			}
		}
	}
}

open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_ASMblock_P_unjusted.bedGraph");
print OUT @normal_p;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_ASMblock_P_unjusted.bedGraph");
print OUT @tumor_p;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_ASMblock_P_adjusted.bedGraph");
print OUT @normal_p_adjust;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_ASMblock_P_adjusted.bedGraph");
print OUT @tumor_p_adjust;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_ASMblock_methyRatio.bedGraph");
print OUT @tumor_methy;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_ASMblock_methyRatio.bedGraph");
print OUT @normal_methy;
close(OUT);
